package booksystem;

import java.util.ArrayList;
import java.util.Scanner;
/**
 * @TODO
 * @Author xu
 * 2025/3/17
 */
public class MainUi {
    private static ArrayList<Book> bookList = new ArrayList<>(); // 用于存储书籍的列表
    private static Scanner scanner = new Scanner(System.in); // Scanner对象用于输入

    public static void main(String[] args) {
        while (true) {
            System.out.println("\n--- 图书管理系统 ---");
            System.out.println("1. 查看书籍");
            System.out.println("2. 添加书籍");
            System.out.println("3. 删除书籍");
            System.out.println("4. 修改书籍");
            System.out.println("5. 退出");
            System.out.print("请选择操作 (1-5): ");

            int choice = scanner.nextInt(); // 读取用户选择
            scanner.nextLine(); // 清空输入缓冲区

            switch (choice) {
                case 1:
                    viewBooks(); // 查询所有书籍
                    break;
                case 2:
                    addBook(); // 添加书籍
                    break;
                case 3:
                    deleteBook(); // 删除书籍
                    break;
                case 4:
                    updateBook(); // 修改书籍
                    break;
                case 5:
                    System.out.println("退出系统。");
                    scanner.close();
                    return; // 退出程序
                default:
                    System.out.println("无效的选择，请重新输入。");
            }
        }
    }

    private static void viewBooks() {
        if (bookList.isEmpty()) {
            System.out.println("没有书籍可显示。");
        } else {
            System.out.println("书籍列表：");
            for (Book book : bookList) {
                System.out.println(book);
            }
        }
    }

    private static void addBook() {
        System.out.print("请输入书名: ");
        String title = scanner.nextLine();
        System.out.print("请输入作者: ");
        String author = scanner.nextLine();
        System.out.print("请输入ISBN: ");
        String isbn = scanner.nextLine();

        Book book = new Book(title, author, isbn);
        bookList.add(book);
        System.out.println("书籍已添加。");
    }

    private static void deleteBook() {
        System.out.print("请输入要删除的书籍ISBN: ");
        String isbn = scanner.nextLine();

        for (int i = 0; i < bookList.size(); i++) {
            if (bookList.get(i).getIsbn().equals(isbn)) {
                bookList.remove(i);
                System.out.println("书籍已删除。");
                return;
            }
        }
        System.out.println("未找到ISBN为" + isbn + "的书籍。");
    }

    private static void updateBook() {
        System.out.print("请输入要修改的书籍ISBN: ");
        String isbn = scanner.nextLine();

        for (Book book : bookList) {
            if (book.getIsbn().equals(isbn)) {
                System.out.print("请输入新的书名 (当前: " + book.getTitle() + "): ");
                String newTitle = scanner.nextLine();
                System.out.print("请输入新的作者 (当前: " + book.getAuthor() + "): ");
                String newAuthor = scanner.nextLine();

                book.setTitle(newTitle);
                book.setAuthor(newAuthor);
                System.out.println("书籍信息已更新。");
                return;
            }
        }
        System.out.println("未找到ISBN为" + isbn + "的书籍。");
    }





















}



